// pages/index/index.js
var Moment = require("../../utils/moment.js");
var DATE_LIST = [];
var DATE_YEAR = new Date().getFullYear()
var DATE_MONTH = new Date().getMonth() + 1
var DATE_DAY = new Date().getDate()
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //日期
    showAddGuestInformation: false,
    detail: null,
    user: null,
    resident: null,
    banner: '',
    night: 0,
    is_use_coupon: false,
    // 折扣内容
    is_use_discount: false,
    // 折扣
    discount: null,
    // 折扣列表
    discounts: [],
    showDiscount: false,
    // 其他
    queryString: {
      product_id: 0, //民宿ID ,
      begin_time: "", //入住时间
      leave_time: "", //离开时间
      night: 1, //入住晚数
      remark: "", //留言
      is_deduction: 0, //是否使用储备金抵扣0=否，1=是
      coupon_id: 0,
      discount_id: 0,
      price: 0, //民宿单价,
      resident_id: 0
    },
    coupon: null,
    pay_info: {
      description: 0,
      pay_price: 0,
    }
  },

  /**
   * 输入绑定
   */
  onInput(event) {
    this.setData({
      'queryString.remark': event.detail
    })
  },

  /**
   * 创建订单
   */
  createOrder() {
    let _this = this;
    app.loading();
    app.HttpsRequest('user.order/create', 'POST', _this.data.queryString).then(ret => {
      wx.removeStorageSync('coupon_id');
      wx.removeStorageSync('resident_id');
      if (ret) {
        wx.hideLoading();
        let param = 'order_id=' + ret.data.order_id;
        app.navigateTo('/pages/submitOrder/payment/index?' + param)
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let getDate = wx.getStorageSync("ROOM_SOURCE_DATE");
    //计算时间差距；
    let sTime = new Date(getDate.checkInDate.replace(/-/g, "/"));
    let eTime = new Date(getDate.checkOutDate.replace(/-/g, "/"));

    let coupon_id = wx.getStorageSync('coupon_id')
    let resident_id = wx.getStorageSync('resident_id')
    this.setData({
      'queryString.begin_time': getDate.checkInDate,
      'queryString.leave_time': getDate.checkOutDate,
      'queryString.night': parseInt((eTime.getTime() - sTime.getTime())) / (1000 * 60 * 60 * 24),
      'queryString.coupon_id': coupon_id ? coupon_id : 0,
      'queryString.resident_id': resident_id ? resident_id : 0,
      'queryString.discount_id': this.data.discount ? this.data.discount.discount_id : 0,
      resident: null,
      coupon: null
    });
    this.getDetail();
    this.getProductDiscount();

    if (this.data.queryString.coupon_id) {
      this.getCoupon();
    }
  },

  /**
   * 获取折扣
   */
  getProductDiscount() {
    app.loading()
    let _this = this;
    let param = _this.data.param;
    if (_this.data.product_id) {
      param.product_id = _this.data.product_id;
    }
    app.HttpsRequest('discount/index', 'GET', param).then(ret => {
      wx.hideLoading();
      if (ret) {
        _this.setData({
          discounts: ret.data
        })
      }
    })
  },


  /**
   * 展示隐藏
   */
  handleShowDiscount() {
    this.setData({
      showDiscount: !this.data.showDiscount
    })
  },

  /**
   *
   * @param event
   */
  handleSelectDiscount(event) {
    let key = event.currentTarget.dataset.key;
    let discount = this.data.discounts[key];
    discount.label = '';
    discount.detail.map((item, index) => {
      discount.label += item.day + '天以上享' + (item.discount / 10) + '折' + (index + 1 < discount.detail.length ? "，" : "。")
    })
    this.setData({
      discount: discount,
      showDiscount: !this.data.showDiscount,
      ['queryString.discount_id']: discount.discount_id
    })
    this.getDetail()
  },

  /**
   * 改变储备金抵扣状态；
   */
  changeDeduction({
                    detail
                  }) {
    // 需要手动对 checked 状态进行更新
    this.setData({
      ['queryString.is_deduction']: detail
    });
    this.getDetail()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let _this = this;
    _this.setData({
      'queryString.product_id': options.product_id ? options.product_id : 8,
    })
    //设缓存缓存起来的日期
    wx.setStorage({
      key: 'ROOM_SOURCE_DATE',
      data: {
        checkInDate: Moment(new Date()).format('YYYY-MM-DD'),
        checkOutDate: Moment(new Date()).add(1, 'day').format('YYYY-MM-DD')
      }
    });
  },

  /**
   * 获取优惠券
   */
  getCoupon() {
    let _this = this;
    app.HttpsRequest('user.coupon/detail', 'GET', {
      id: _this.data.queryString.coupon_id
    }).then(ret => {
      if (ret) {
        _this.setData({
          coupon: ret.data
        })
      }
    })
  },

  /**
   * 获取详情；
   */
  getDetail() {
    let _this = this;
    let param = _this.data.queryString;
    param.is_deduction = param.is_deduction ? 1 : 0
    app.loading();
    app.HttpsRequest('user.order/confirmOrder', 'POST', param).then(ret => {
      if (ret) {
        wx.hideLoading()
        _this.setData({
          detail: ret.data.product,
          user: ret.data.user,
          resident: ret.data.resident,
          pay_info: ret.data.pay_info,
          is_use_coupon: ret.data.is_use_coupon,
          'queryString.price': ret.data.product.price
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})